package hu.szyrt.bug.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Crypto {

	public static String sha256Hex(String value){
		return bytesToHexString(sha256(value));
	}
	
	public static String sha1Hex(String value){
		return bytesToHexString(sha1(value));
	}
	
	public static byte[] sha256(String value){
		try {
			MessageDigest digest = MessageDigest.getInstance("SHA-256");
						  digest.reset();
						  digest.update(value.getBytes());
			return digest.digest();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static byte[] sha1(String value){
		try {
			MessageDigest digest = MessageDigest.getInstance("SHA-1");
						  digest.reset();
						  digest.update(value.getBytes());
			return digest.digest();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	private static String bytesToHexString(byte[] bytes) {
        // http://stackoverflow.com/questions/332079
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < bytes.length; i++) {
            String hex = Integer.toHexString(0xFF & bytes[i]);
            if (hex.length() == 1) {
                sb.append('0');
            }
            sb.append(hex);
        }
        return sb.toString();
    }
	
	
}
